﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace ElsProp.Model
{
    public class MainWindowModel : ObservableObject
    {
        /// <summary>
        /// 装备名称
        /// </summary>
        private string _propName;
        /// <summary>
        /// 装备名称
        /// </summary>
        public string propName { get => _propName; set => SetProperty(ref _propName, value); }

        /// <summary>
        /// 用户选择
        /// </summary>
        private List<UserChangeMode> _userChange;
        /// <summary>
        /// 用户选择
        /// </summary>
        public List<UserChangeMode> userChange { get => _userChange; set => SetProperty(ref _userChange, value); }

        /// <summary>
        /// 物品属性
        /// </summary>
        private ObservableCollection<ItemProperties> _itemProperties;
        /// <summary>
        /// 物品属性
        /// </summary>
        public ObservableCollection<ItemProperties> itemProperties { get => _itemProperties; set => SetProperty(ref _itemProperties, value); }
        /// <summary>
        /// 物品套装属性
        /// </summary>
        private ObservableCollection<ItemProperties> _itemSuitProperties;
        /// <summary>
        /// 物品套装属性
        /// </summary>
        public ObservableCollection<ItemProperties> itemSuitProperties { get => _itemSuitProperties; set => SetProperty(ref _itemSuitProperties, value); }
        /// <summary>
        /// 物品属性
        /// </summary>
        private ItemProperties _itemPropertie;
        /// <summary>
        /// 物品属性
        /// </summary>
        public ItemProperties itemPropertie
        {
            get => _itemPropertie;
            set
            {
                SetProperty(ref _itemPropertie, value);
                ItemPropertiesName = _itemPropertie.PropertiesName;
                ItemPropertiesValue = _itemPropertie.PropertiesValue;
            }
        }
        /// <summary>
        /// 属性名称
        /// </summary>
        private string _ItemPropertiesName;
        /// <summary>
        /// 属性名称
        /// </summary>
        public string ItemPropertiesName { get => _ItemPropertiesName; set => SetProperty(ref _ItemPropertiesName, value); }
        /// <summary>
        /// 属性值
        /// </summary>
        private string _ItemPropertiesValue;
        /// <summary>
        /// 属性值
        /// </summary>
        public string ItemPropertiesValue { get => _ItemPropertiesValue; set => SetProperty(ref _ItemPropertiesValue, value); }

        /// <summary>
        /// 装备列表
        /// </summary>
        private List<PropModel> _propModels;
        /// <summary>
        /// 装备列表
        /// </summary>
        public List<PropModel> propModels { get => _propModels; set => SetProperty(ref _propModels, value); }
        /// <summary>
        /// 套装
        /// </summary>
        private List<PropModel> _suit;
        /// <summary>
        /// 套装
        /// </summary>
        public List<PropModel> suit { get => _suit; set => SetProperty(ref _suit, value); }

        public ICommand AddCommand { get; set; }

        public ICommand DelCommand { get; set; }

        public ICommand SaveCommand { get; set; }

        public MainWindowModel()
        {

            userChange = new List<UserChangeMode>();
            foreach (var item in Data.user)
            {
                userChange.Add(new UserChangeMode()
                {
                    Change = false,
                    userName = item
                });
            }
            itemPropertie = new ItemProperties();
            itemProperties = new();
            AddCommand = new RelayCommand(AddCommandMethod);
            DelCommand = new RelayCommand(DelCommandMethod);
            SaveCommand = new RelayCommand(SaveCommandMethod);
            LoadZhuangBeiMethod();
        }
        private void LoadZhuangBeiMethod()
        {
            List<PropModel> propModels = new List<PropModel>();
            if (!Directory.Exists(Data.path))
            {
                Directory.CreateDirectory(Data.path);
            }
            var files = Directory.GetFiles(Data.path);
            foreach (var file in files)
            {
                var zhuangbei = File.ReadAllText(file);
                try
                {
                    propModels.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<PropModel>(zhuangbei));
                }
                catch
                {

                }
            }
            foreach (var propModel in Data.propModels) {
                var has= propModels.Find(x => x.Suit == propModel.Suit && x.Name == propModel.Name);
                if (has==null)
                {
                    propModels.Add(propModel);
                }
            }
            this.propModels = propModels;
            List<PropModel> suit = new List<PropModel>();
           var gd=  propModels.GroupBy(x => x.Suit);
            foreach (var item in gd)
            {
                suit.Add(item.First());
            }
            this.suit = suit;
        }
        private void SaveCommandMethod()
        {
            if (string.IsNullOrEmpty(propName))
            {
                MessageBox.Show("名字得写");
                return;
            }
            string path = "prop\\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            PropModel propModel = new PropModel()
            {
                Name = propName,
            };
            var us = userChange.FindAll(x => x.Change);
            propModel.Properties = itemProperties;
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(propModel);
            var has = File.Exists(path+propModel.Name + ".json");
            if (has)
            {
                if (MessageBox.Show("存在同名文件，是否覆盖","提示",MessageBoxButton.OKCancel)==MessageBoxResult.OK)
                {
                    File.WriteAllText(path + propModel.Name + ".json", json);
                }
            }
            else
            {
                File.Create(path + propModel.Name + ".json").Close() ;
                File.WriteAllText(path + propModel.Name + ".json", json);
            }
        }

        private void DelCommandMethod()
        {
            var pro = itemProperties.First(x => x.PropertiesName == itemPropertie.PropertiesName);
            itemProperties.Remove(pro);
        }

        private void AddCommandMethod()
        {
            var newitem = new ItemProperties()
            {
                PropertiesName = ItemPropertiesName,
                PropertiesValue = ItemPropertiesValue,
            };
            newitem.DeleteThis += Newitem_DeleteThis;
            itemProperties.Add(newitem);
            //var dst = new ObservableCollection<ItemProperties>();
            //dst.Add(itemProperties);
            //var newitem = new ItemProperties()
            //{
            //    PropertiesName = itemPropertie.PropertiesName,
            //    PropertiesValue = itemPropertie.PropertiesValue,
            //};
            //newitem.DeleteThis += Newitem_DeleteThis;
            //dst.Add(newitem);
            //itemProperties = dst;

        }

        private void Newitem_DeleteThis(ItemProperties obj)
        {
            foreach (var item in itemProperties)
            {
                if (item.PropertiesName == obj.PropertiesName)
                {
                    itemProperties.Remove(item);
                    break;
                }
            }
        }
    }
}
